package com.weipai.education.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;

/**
 * @author songmengqiang
 * @create 2018-09-13 19:09
 **/

public class HttpClientUtils {

    private static CloseableHttpClient httpClient = null;
    private static RequestConfig requestConfig = null;

    static {
        httpClient = HttpClients.createDefault();

        //设置http的状态参数
        requestConfig = RequestConfig.custom()
                .setSocketTimeout(30000)
                .setConnectTimeout(30000)
                .setConnectionRequestTimeout(30000)
                .build();
    }

    public static CloseableHttpClient getClient() {
//        return httpBuilder.build();
        return httpClient;
    }


    public static HttpUriRequest getRequestMethod(Map<String, String> map, String url, String method) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        if (map == null) {
            map = new HashMap<>();
        }
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> e : entrySet) {
            String name = e.getKey();
            String value = e.getValue();
            NameValuePair pair = new BasicNameValuePair(name, value);
            params.add(pair);
        }
        HttpUriRequest reqMethod = null;
        if ("post".equals(method)) {
            reqMethod = RequestBuilder.post().setUri(url)
                    .addParameters(params.toArray(new BasicNameValuePair[params.size()]))
                    .setConfig(requestConfig).build();
        } else if ("get".equals(method)) {
            reqMethod = RequestBuilder.get().setUri(url)
                    .addParameters(params.toArray(new BasicNameValuePair[params.size()]))
                    .setConfig(requestConfig).build();
        }
        return reqMethod;
    }

    public static <T> T getJson(String url, Class<T> clz) throws IOException {
        HttpUriRequest request = getRequestMethod(null, url, "get");
        HttpResponse response = httpClient.execute(request);
        response.getEntity();
        String res = EntityUtils.toString(response.getEntity(), "utf-8");
        return JSON.parseObject(res, clz);
    }

//    public static void main(String[] args) {
//        try {
//            Map<String, Object> obj = getJson("https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=6228430120000000000&cardBinCheck=true", Map.class);
//            System.out.println(obj);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

}
